﻿<UserControl x:Class="DaxStudio.UI.Views.ServerTimesView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:conv="clr-namespace:DaxStudio.UI.Converters"
             xmlns:behaviours="clr-namespace:DaxStudio.UI.Behaviours"
             xmlns:extenders="clr-namespace:DaxStudio.UI.AttachedProperties"             
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="800"
             BorderBrush="DimGray" BorderThickness="1">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="pack://application:,,,/DaxStudio.UI;Component/Resources/IconResourceDictionary.xaml" />
                <ResourceDictionary  Source="pack://application:,,,/DaxStudio.UI;Component/Styles/BusyPanel.xaml" />
                <ResourceDictionary  Source="pack://application:,,,/DaxStudio.UI;Component/Styles/LabelSpinnerStyle.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <conv:EventSubclassConverter x:Key="EventSubclass"></conv:EventSubclassConverter>
            <conv:QuerySingleLineConverter x:Key="QuerySingleLine"></conv:QuerySingleLineConverter>
            <conv:QueryMultiLineConverter x:Key="QueryMultiLine"></conv:QueryMultiLineConverter>
            <conv:MultiplyConverter x:Key="MultiConverter"></conv:MultiplyConverter>
            <conv:BoolToHiddenConverter x:Key="BoolToVisibilityConverter" />
        </ResourceDictionary>
        
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="130"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="2*" MinWidth="400"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="{Binding TextColumnWidth}" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <ScrollViewer Background="White" VerticalScrollBarVisibility="Auto" Grid.Column="0"  Grid.RowSpan="3">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
 
                </Grid.RowDefinitions>
                <Grid Background="White" Grid.Row="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
      
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="Total" Grid.Row="0" Grid.Column="0" Padding="0,8,0,0" HorizontalAlignment="Center" FontWeight="Bold"/>
                    <TextBlock Grid.Row="1" Grid.Column="0" Padding="0,0,0,5" HorizontalAlignment="Center"
                       Text="{Binding TotalDuration, StringFormat='#,0 ms'}" />

                    <TextBlock Text="Total CPU" Grid.Row="0" Grid.Column="1" Padding="0,8,0,0" HorizontalAlignment="Center" FontWeight="Bold"/>
                    <TextBlock Grid.Row="1" Grid.Column="1" Padding="0,0,0,5" HorizontalAlignment="Center"
                       Text="{Binding TotalCpuDuration, StringFormat='#,0 ms'}"  />


                    <TextBlock Text="FE" Grid.Row="2" Grid.Column="0" Padding="0,8,0,0" HorizontalAlignment="Center" FontWeight="Bold"/>
                    <TextBlock Grid.Row="3" Grid.Column="0" Padding="0" HorizontalAlignment="Center"
                       Text="{Binding FormulaEngineDuration, StringFormat='#,0 ms'}" />
                    <TextBlock Grid.Row="4" Grid.Column="0" Padding="0,0,0,5" HorizontalAlignment="Center"
                   Text="{Binding FormulaEngineDurationPercentage, StringFormat='0.0%'}"   FontSize="10" Foreground="#3571AD" />

                    <TextBlock Text="SE" Grid.Row="2" Grid.Column="1" Padding="0,8,0,0" HorizontalAlignment="Center" FontWeight="Bold"/>
                    <TextBlock Grid.Row="3" Grid.Column="1" Padding="0" HorizontalAlignment="Center"
                       Text="{Binding StorageEngineDuration, StringFormat='#,0 ms'}" />
                    <TextBlock Grid.Row="4" Grid.Column="1" Padding="0,0,0,5" HorizontalAlignment="Center"
                   Text="{Binding StorageEngineDurationPercentage, StringFormat='0.0%'}"  FontSize="10" Foreground="#3571AD"/>


                    <TextBlock Text="SE CPU" Grid.Row="5" Grid.Column="0" Padding="0,8,0,0" HorizontalAlignment="Center" FontWeight="Bold"/>
                    <TextBlock Grid.Row="6" Grid.Column="0" Padding="0,0,0,5" HorizontalAlignment="Center"
                       Text="{Binding StorageEngineCpu, StringFormat='#,0 ms'}"  />

                    <TextBlock Text="SE Queries" Grid.Row="5" Grid.Column="1" Padding="0,8,0,0" HorizontalAlignment="Center" FontWeight="Bold"/>
                    <TextBlock Grid.Row="6" Grid.Column="1" Padding="0,0,0,5" HorizontalAlignment="Center"
                       Text="{Binding StorageEngineQueryCount, StringFormat='#,0'}"/>
                    
                    <TextBlock Text="SE Cache" Grid.Row="7" Grid.Column="0" Padding="0,8,0,0" HorizontalAlignment="Center" FontWeight="Bold"/>

                    <TextBlock Grid.Row="8" Grid.Column="0" Padding="0" HorizontalAlignment="Center"
                       Text="{Binding VertipaqCacheMatches, StringFormat='#,0'}" />
                    <TextBlock Grid.Row="9" Grid.Column="0" Padding="0,0,0,5" HorizontalAlignment="Center" 
                   Text="{Binding VertipaqCacheMatchesPercentage, StringFormat='0.0%'}"  FontSize="10" Foreground="#3571AD"/>
                </Grid>
                
                
            </Grid>
        </ScrollViewer>
        <GridSplitter Width="5" Grid.Column="1" 
                      Grid.RowSpan="3"
                      ResizeDirection="Columns" 
                      Grid.Row="0"
                      Height="Auto" 
                    HorizontalAlignment="Stretch" 
                    VerticalAlignment="Stretch" 
                    Margin="0"/>
        <DataGrid Name="ListEvents" Grid.Column="2" SelectedItem="{Binding SelectedEvent}" HorizontalAlignment="Stretch" AutoGenerateColumns="False"
                  Grid.Row="0"
                  behaviours:GridViewColumnResize.Enabled="True" VerticalAlignment="Stretch" Background="White" Width="Auto"
                  ItemsSource="{Binding StorageEngineEvents}" 
                  SelectionMode="Extended"
                  SelectionUnit="FullRow"
                  HeadersVisibility="Column"
                  ClipboardCopyMode="IncludeHeader"
                  ScrollViewer.CanContentScroll="False"
                  FrozenColumnCount="1"
                  GridLinesVisibility="None" BorderThickness="1,0,0,0" BorderBrush="{x:Null}" >
            <DataGrid.ContextMenu>
                <ContextMenu>
                    <ContextMenu.ItemsSource>
                        <CompositeCollection>
                            <MenuItem Command="Copy" Icon="{StaticResource CopyIcon}"></MenuItem>
                            <Separator/>
                            <MenuItem Command="SelectAll"></MenuItem>
                            <Separator />
                            <MenuItem Header="Scan" IsCheckable="True" IsChecked="{Binding ServerTimingDetails.ShowScan}" />
                            <MenuItem Header="Cache" IsCheckable="True" IsChecked="{Binding ServerTimingDetails.ShowCache}" />
                            <MenuItem Header="Internal" IsCheckable="True" IsChecked="{Binding ServerTimingDetails.ShowInternal}" />
                        </CompositeCollection>
                    </ContextMenu.ItemsSource>
                </ContextMenu>
            </DataGrid.ContextMenu>

            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="VerticalContentAlignment" Value="Center" />
                </Style>
                <Style x:Key="AlignRight" TargetType="{x:Type TextBlock}">
                    <Setter Property="HorizontalAlignment" Value="Right" />
                </Style>
            </DataGrid.Resources>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontWeight"
                  Value="SemiBold" />
                </Style>
            </DataGrid.ColumnHeaderStyle>
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Padding" Value="5,0,5,0"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridCell}">
                                <Border Padding="{TemplateBinding Padding}" 
                                        BorderBrush="{TemplateBinding BorderBrush}" 
                                        BorderThickness="{TemplateBinding BorderThickness}" 
                                        Background="{TemplateBinding Background}" 
                                        SnapsToDevicePixels="True">
                                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <EventSetter Event="Control.RequestBringIntoView" Handler="DataGrid_RequestBringIntoView"  />
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Line" Width="35" IsReadOnly="True" Binding="{Binding RowNumber, StringFormat='#,#;;'}" ElementStyle="{StaticResource AlignRight}" />
                <DataGridTextColumn Header="Subclass" Width="60" IsReadOnly="True" Binding="{Binding Subclass, Converter={StaticResource EventSubclass}}" />
                <DataGridTextColumn Header="Duration" IsReadOnly="True" Width="60" Binding="{Binding Duration, StringFormat='#,0'}" ElementStyle="{StaticResource AlignRight}" />
                <DataGridTextColumn Header="CPU" IsReadOnly="True" Width="60" Binding="{Binding CpuTime, StringFormat='#,0'}" ElementStyle="{StaticResource AlignRight}" />
                <DataGridTextColumn Header="Query" behaviours:GridViewColumnResize.Width="Auto" 
                                    IsReadOnly="True" Binding="{Binding Query, Converter={StaticResource QuerySingleLine}}"/>

            </DataGrid.Columns>
            <DataGrid.Style>

                <Style>
                    <!--<Setter Property="Grid.RowSpan" Value="{Binding Parent.TextGridRowSpan}" />-->
                    <Setter Property="Grid.RowSpan" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.TextGridRowSpan}" />
                </Style>
                
            </DataGrid.Style>
        </DataGrid>
        <GridSplitter Height="5" Grid.Row="1" Grid.Column="2" ResizeDirection="Rows" HorizontalAlignment="Stretch" 
                      Visibility="{Binding Converter={StaticResource BoolToVisibilityConverter}, Path=ServerTimingDetails.LayoutBottom}" />
        <GridSplitter Width="5" Grid.Column="3" 
                      Grid.RowSpan="3"
                      ResizeDirection="Columns" 
                      Grid.Row="0"
                      Height="Auto" 
                    HorizontalAlignment="Stretch" 
                    VerticalAlignment="Stretch" 
                      Visibility="{Binding Converter={StaticResource BoolToVisibilityConverter}, Path=ServerTimingDetails.LayoutRight}"
                    Margin="0"/>
        <TextBox  Padding="5,5,5,5"
                  
                 MinWidth="200"
                  MinHeight="100"
                     TextWrapping="Wrap"
                     Text="{Binding ElementName=ListEvents, Path=SelectedItem.Query, Mode=OneWay}"
                         BorderThickness="0"
                         IsReadOnly="True">
            <TextBox.Style>
                <Style>
                    <Setter Property="Grid.Row" Value="{Binding TextGridRow}" />
                    <Setter Property="Grid.RowSpan" Value="{Binding TextGridRowSpan}" />
                    <Setter Property="Grid.Column" Value="{Binding TextGridColumn}" />
                </Style>
            </TextBox.Style>
            <TextBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Command="Copy" Icon="{StaticResource CopyIcon}"></MenuItem>
                    <Separator/>
                    <MenuItem Command="SelectAll"></MenuItem>
                </ContextMenu>
            </TextBox.ContextMenu>
        </TextBox>

        <!-- Busy overlay -->
        <Border 
                Visibility="{Binding IsBusy, Converter={StaticResource BoolToVisibilityConverter}}" 
                Grid.RowSpan="3" 
            Grid.ColumnSpan="5"
            Style="{StaticResource BusyPanel}">
                
            <Grid>

                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                    <!--
                    <Views:ucSpinner Foreground="White" Margin="0,0,10,0" />
                    <TextBlock Margin="0" TextWrapping="Wrap" Text="Running Query..." HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18" FontWeight="Bold" Foreground="#7E000000"/>
                    -->
                    <Label Style="{StaticResource LabelSpinnerStyle}" Height="30" VerticalContentAlignment="Center"  FontSize="18" FontWeight="Bold" Foreground="#D0000000">Running Query...</Label>
                </StackPanel>
            </Grid>
        </Border>
    </Grid>
</UserControl>
